import { Axios } from '@axios/base'
import { HttpStatus } from '@axios/base/http_status';
import { User } from '@entity/user';
import { SimpleEntity } from '@entity/common';
import { PrivateMsg } from '@entity/chat';
import { WebSocketRequest } from '@entity/ws';

export class WebSocketPrivateMsg extends Axios {

    constructor() {
        super('ws/private');
    }

    /**
     * GET /ws/private/sendToUser
     * 向一个用户发送私信
     * @param receiver 接收方
     * @param data 私信
     */
    async sendToUser(receiver: User | SimpleEntity, data: PrivateMsg): Promise<Boolean> {
        let request: WebSocketRequest<PrivateMsg> = new WebSocketRequest(receiver, data);
        const result = await this.post<Boolean>('/sendToUser', request);
        return result.status == HttpStatus.SUCCESS ? result.data : false;
    }
    
    /**
     * GET /ws/private/readAll
     * 获取指定 id 用户未读取的私信消息
     * @param id 用户 id
     */
    async readPrivateMsg(id: number, unread: boolean): Promise<Array<PrivateMsg>> {
        const result = await this.get<Array<PrivateMsg>>('/readAll', {
            params: {
                id: id,
                unreadOnly: unread
            }
        });
        return result.status == HttpStatus.SUCCESS ? result.data : [];
    }

    /**
     * GET /ws/private/readed
     * 将指定发送人的私信消息变为已读   
     * @param id 消息 id
     */
    doReadMsg(id: string): void {
            this.get<Boolean>('/readed', {
            params: {
                id: id
            }
        });
    }

}